#include <iostream>
#include "Member.h"
#include "MemberDAO.h"

using namespace everest::sample;
using namespace everest::sample;
using namespace std;

int main(int argc, char **argv)
{
	Member m1;
	Member m2;
	Member m3;
	
	m1.setId(100);
	m1.setUserName("ORM-USER-1");
	m1.setPassword("ORM-PASS-1");
	m1.setCreateDate("2014-5-12 9:08:09");
	m2.setId(101);
	m2.setUserName("ORM-USER-2");
	m2.setPassword("ORM-PASS-2");
	m2.setCreateDate("2014-5-12 9:08:09");
	m3.setId(102);
	m3.setUserName("ORM-USER-3");
	m3.setPassword("ORM-PASS-3");
	m3.setCreateDate("");	

	otl_connect::otl_initialize();
	otl_connect conn;
		
	try {	
		conn.rlogon("DSN=EVEREST;UID=shi;PWD=shiyimin", 0);
		conn.auto_commit_off();
		MemberDAO dao(&conn, 1);
		
		dao.remove(m1);
		dao.remove(m2);
		dao.remove(m3);

		dao.insert(m1);
		dao.insert(m2);
		dao.insert(m3);

		m1.setUserName("ORM-USER-1-1");
		dao.update(m1);

		dao.flush();
		dao.clear();
		
		m1.setId(102);
		int ret = dao.select(m1);
		cout<<"select by id = 102, result = "<<ret<<endl;
		cout<<"\t user="<<m1.getUserName()<<",pass="<<m1.getPassword()<<", creatdate = "<<m1.getCreateDate()<<endl;
		conn.commit();

		vector<Member> vec;
		ret = dao.select(vec, "username like 'ORM%'");
		cout<<"Select Where returns "<<ret<<endl;
		for(size_t i = 0; i < vec.size();++i) {
			Member& m = vec[i];
			cout<<"id="<<m.getId()<<";user="<<m.getUserName();
			cout<<";pass="<<m.getPassword()<<";";
			cout<<"creatdate="<<m.getCreateDate()<<endl;
		}

	} catch (const otl_exception& e) {
		cout<<e.code<<": "<<e.msg<<endl;
		cout<<"stmt: "<<e.stm_text<<endl;
		conn.rollback();
	}
	
	return 0;
}

